package cn.com.zetatech.loader.util;

import cn.com.zetatech.loader.entity.DefectBO;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class WaferDefectMapUtils {

    public static class Defect {
        private float coorX;
        private float coorY;
        private String color;
        private int size;

        public Defect(float coorX, float coorY, String color, int size) {
            this.coorX = coorX;
            this.coorY = coorY;
            this.color = color;
            this.size = size;
        }

        public float getCoorX() {
            return coorX;
        }

        public float getCoorY() {
            return coorY;
        }

        public String getColor() {
            return color;
        }

        public int getSize() {
            return size;
        }
    }

    public static BufferedImage drawDefects(List<Defect> defects, float waferDiameter, int imageSize, int lineWidth) {
        int mapWidth = imageSize + lineWidth;
        int mapHeight = imageSize + lineWidth;

        // 确认圆心坐标
        float centerX = mapWidth / 2.0f;
        float centerY = mapHeight / 2.0f;

        BufferedImage b = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g = b.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 设置透明背景色
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(0, 0, mapWidth, mapHeight);
        g.setComposite(AlphaComposite.SrcOver);

        // 画白色圆形背景
        Ellipse2D.Float rect = new Ellipse2D.Float(lineWidth / 2f, lineWidth / 2f, mapWidth - lineWidth, mapHeight - lineWidth);
        g.setColor(Color.WHITE);
        g.fill(rect);

        // 设置剪切区域为圆形
        g.setClip(rect);

        // 画圆形边界
        g.setColor(new Color(219, 241, 253));
        g.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g.draw(rect);

        // 画缺陷
        for (Defect defect : defects) {
            g.setColor(Color.decode(defect.getColor()));

            // 将缺陷坐标转换为图像坐标
            float defectX = centerX + (defect.getCoorX() * (mapWidth - lineWidth) / waferDiameter);
            float defectY = centerY + (defect.getCoorY() * (mapHeight - lineWidth) / waferDiameter);

            // 由于y坐标从图像顶部开始计算，需调整y坐标
            defectY = mapHeight - defectY;

            g.fillOval((int) defectX - defect.getSize() / 2, (int) defectY - defect.getSize() / 2, defect.getSize(), defect.getSize());
        }

        g.dispose();
        return b;
    }

    /***
     *  根据defect数据绘图
     * @param defects 缺陷点
     * @return
     */
    public static BufferedImage drawDefectImage(List<DefectBO> defects) {
        // 12寸晶圆默认直径（以纳米为单位，等同于300毫米）
        BigInteger waferDiameter = new BigInteger("300000000");
        // 设置图片大小
        int imageSize = 500;
        // 设置圆边框线宽
        int lineWidth = 2;
        int mapWidth = imageSize + lineWidth;
        int mapHeight = imageSize + lineWidth;

        // 确认圆心坐标
        float centerX = mapWidth / 2.0f;
        float centerY = mapHeight / 2.0f;

        BufferedImage b = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g = b.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 设置透明背景色
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(0, 0, mapWidth, mapHeight);
        g.setComposite(AlphaComposite.SrcOver);

        // 画白色圆形背景
        Ellipse2D.Float rect = new Ellipse2D.Float(lineWidth / 2f, lineWidth / 2f, mapWidth - lineWidth, mapHeight - lineWidth);
        g.setColor(Color.WHITE);
        g.fill(rect);

        // 设置剪切区域为圆形
        g.setClip(rect);

        // 画圆形边界
        g.setColor(new Color(219, 241, 253));
        g.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g.draw(rect);

        // 画缺陷
        for (DefectBO defect : defects) {
            g.setColor(Color.decode(defect.getColor()));

            // 将缺陷坐标转换为BigInteger
            BigInteger waferX = BigInteger.valueOf(defect.getWaferX());
            BigInteger waferY = BigInteger.valueOf(defect.getWaferY());
            BigInteger mapWidthBI = BigInteger.valueOf(mapWidth - lineWidth);
            BigInteger mapHeightBI = BigInteger.valueOf(mapHeight - lineWidth);

            // 计算图像坐标
            BigInteger defectX = waferX.multiply(mapWidthBI).divide(waferDiameter).add(BigInteger.valueOf((long) centerX));
            BigInteger defectY = waferY.multiply(mapHeightBI).divide(waferDiameter).add(BigInteger.valueOf((long) centerY));
            defectY = BigInteger.valueOf(mapHeight).subtract(defectY);

            g.fillOval(defectX.intValue(), defectY.intValue(), 3, 3);
        }

        g.dispose();
        return b;
    }

    public static void main(String[] args) {
        // 示例缺陷数据
        List<Defect> defects = new ArrayList<>();
        defects.add(new Defect(0, 0, "#FF0000", 4));
        defects.add(new Defect(10, 10, "#0000FF", 4));
        defects.add(new Defect(-10, -10, "#00FF00", 4));

        BufferedImage image = drawDefects(defects, 200, 500, 5);

        // 保存图像到文件
        File outFile = new File("wafer_map.png");
        try {
            ImageIO.write(image, "PNG", outFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
